/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mobdoki.server.servlet.user;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mobdoki.server.Connect;
import mobdoki.server.JSONObj;
import mobdoki.server.Sessions;

/**
 *
 * @author Andreas
 */
public class RefreshSession extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        JSONObj json = new JSONObj();

        String username = request.getParameter("username");
        int password = Integer.parseInt(request.getParameter("password"));
        String ssid = request.getParameter("ssid");
        
        try {
            Class.forName(Connect.driver); //load the driver
            Connection db = DriverManager.getConnection(Connect.url, Connect.user, Connect.pass);
            
            if (db!=null) {
                String sqlText = "SELECT u.id,  ut.name " +
                                 "FROM \"User\" u INNER JOIN \"UserType\" ut ON (u.\"usertypeID\"=ut.id)" + 
                                 "WHERE u.username=? AND u.password=?";
                PreparedStatement ps = db.prepareStatement(sqlText);
                ps.setString(1,username);
                ps.setInt(2,password);
                ResultSet results = ps.executeQuery();
                if (results != null && results.next()) {            // felhasznalo tipusa es ID-ja
                    int userid = results.getInt(1);
                    String usertype = results.getString(2); 
                    
                    int id = Sessions.MySessions().getUserID(ssid);
                    json.setOKMessage("Sikeres meghosszabítás.");
                    if (id==userid) Sessions.MySessions().makeItValid(ssid);
                    else if (id==0) {
                        Calendar calendar = Calendar.getInstance();     // 30 perces munkamenet
                        calendar.add(Calendar.MINUTE, 30);
                        Date validTime = calendar.getTime();
                        Sessions.MySessions().addSession(ssid, userid, username, usertype, validTime);
                    }
                    else json.setErrorMessage("Nem megfelelő adatok.");
                } else throw new Exception();
                
                results.close();
                db.close();
            } else throw new Exception();
        } catch (Exception e) {
            json.setDBError();
        } finally {
            json.write(out);
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
